package com.christophpurrer;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.christophpurrer.shg2.ShakingGestures2Acitivity;
import com.christophpurrer.shg3.ShakeListenerTestActivity3;
import com.christophpurrer.shg4.ShakeActivity;
import com.christophpurrer.shg5.AccelerometerActivity;

/**
 * ShakingGesturesActivity detects shake gestures by using the built in Android
 * accelerometer sensor
 * 
 * Partially based on:
 * http://stackoverflow.com/questions/2317428/android-i-want-to-shake-it
 * 
 * Partially based on:
 * http://developer.android.com/reference/android/hardware/SensorManager.html
 * 
 * Also consider:
 * http://groups.google.com/group/android-developers/browse_thread
 * /thread/beaf6cb6fbfaf68b/e31a9c209f3c96f3?lnk=gst&q=sensor&pli=1
 * 
 * @author Christoph Purrer
 * 
 */
public class ShakingGesturesActivity extends Activity {

	// properties
	private SensorManager sensorManager = null;
	// acceleration apart from gravity
	private float acceleration = 0f;
	// current acceleration including gravity
	private float accelerationCurrent = 0f;
	// last acceleration including gravity
	private float accelerationLast = 0f;

	private final SensorEventListener sensorEventListener = new SensorEventListener() {
		public void onSensorChanged(SensorEvent sensorEvent) {
			float x = sensorEvent.values[0];
			float y = sensorEvent.values[1];
			float z = sensorEvent.values[2];

			accelerationLast = accelerationCurrent;
			accelerationCurrent = (float) Math.sqrt((double) (x * x + y * y + z
					* z));

			float delta = accelerationCurrent - accelerationLast;
			// perform low-cut filter
			acceleration = acceleration * 0.9f + delta;

			shakingGesturesTvCurrentAcceleration.setText(decimalFormat
					.format(acceleration));
		}

		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}
	};

	private DecimalFormat decimalFormat = new DecimalFormat("#.###");

	// fields
	private TextView shakingGesturesTvCurrentAcceleration = null;
	private Button btnSwitch2ShakingGestures2 = null;
	private Button btnSwitch2ShakingGestures3 = null;
	private Button btnSwitch2ShakingGestures4 = null;
	private Button btnSwitch2ShakingGestures5 = null;

	// methods

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// setup the sensorManager
		this.sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		this.sensorManager.registerListener(this.sensorEventListener,
				this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
		this.acceleration = 0.00f;
		this.accelerationCurrent = SensorManager.GRAVITY_EARTH;
		this.accelerationLast = SensorManager.GRAVITY_EARTH;

		// register components
		this.shakingGesturesTvCurrentAcceleration = (TextView) this
				.findViewById(R.id.shakingGesturesTvCurrentAcceleration);

		this.btnSwitch2ShakingGestures2 = (Button) this
				.findViewById(R.id.btnSwitch2ShakingGestures2);
		this.btnSwitch2ShakingGestures2
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(ShakingGesturesActivity.this,
								ShakingGestures2Acitivity.class));
					}
				});

		this.btnSwitch2ShakingGestures3 = (Button) this
				.findViewById(R.id.btnSwitch2ShakingGestures3);
		this.btnSwitch2ShakingGestures3
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(ShakingGesturesActivity.this,
								ShakeListenerTestActivity3.class));
					}
				});

		this.btnSwitch2ShakingGestures4 = (Button) this
				.findViewById(R.id.btnSwitch2ShakingGestures4);
		this.btnSwitch2ShakingGestures4
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(ShakingGesturesActivity.this,
								ShakeActivity.class));
					}
				});

		this.btnSwitch2ShakingGestures5 = (Button) this
				.findViewById(R.id.btnSwitch2ShakingGestures5);
		this.btnSwitch2ShakingGestures5
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						startActivity(new Intent(ShakingGesturesActivity.this,
								AccelerometerActivity.class));
					}
				});
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.sensorManager.registerListener(this.sensorEventListener,
				this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onStop() {
		this.sensorManager.unregisterListener(this.sensorEventListener);
		super.onStop();
	}

}